<?php

namespace App\Models\config;

use App\Models\Model;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\config\Config
 *
 * @property int $id
 * @property string|null $type 数据类型
 * @property int $field_type 0=>不做处理  1=>根据英文逗号切割  2=>根绝回车切割
 * @property string $module 模块标识
 * @property string $name key
 * @property string|null $value value
 * @property int $status 状态
 * @property string $title 配置标题
 * @property string|null $help 帮助标识
 * @property string|null $rule 验证信息
 * @property int $sort
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereFieldType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereHelp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereModule($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereRule($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\config\Config whereValue($value)
 * @mixin \Eloquent
 * @property-read mixed $header_img
 * @property-read mixed $icon
 * @property-read mixed $img
 * @property-read mixed $imgs
 */
class Config extends Model
{
    //
    protected $fillable = [
        'id',
        'type',
        'module',
        'name',
        'value',
        'title',
        'help',
        'rule',
        'field_type'
    ];

    public static function batchSet(string $module, array $configs)
    {
        DB::transaction(function () use ($configs, $module) {
            foreach ($configs as $key => $value) {
                $type = null;
                if (is_array($value)) {
                    $value = trim(implode(',',$value),',');
//                    $value = json_encode($value);
//                    $type = 'json';
                }

                $config = self::where([
                    'name' => $key,
                ])->first();

                if ($config) {
                    $config->value = $value;
                    //    $config->type = $type;
                    $config->save();
                } else {
                    self::create([
                        'module' => $module,
                        'name' => $key,
                        'value' => $value,
                        'type' => $type
                    ]);
                }
            }
        });
    }

    public static function getConfig(string $field)
    {
        if (strstr($field,',')){
            $data = self::whereIn('name',explode(',',$field))->select('name','value','field_type')->cache(10)->get();
            $array = [];
            foreach ($data as $v){
                $array[$v['name']] = self::handelField($v);
            }
            return $array;
        }else{
            $map['name'] = $field;
            return self::handelField(self::where($map)->select('name','value','field_type')->first());
        }
    }

    protected static function handelField($item)
    {

        switch ($item['field_type']){
            case 0:
                $value = $item['value'];
                break;
            case 1:
                $value = explode(',',$item['value']);
                break;
            case 2:
                $value = explode("\r\n",$item['value']);
                break;
        }
        return $value;

    }

}
